//bases on Mark Nelson implementation of GST

package ru.osu.acm.system.sourcediff;

public class Rebro {
    private int beginInd;
    private int endInd;
    private Uzel startUzel;
    private Uzel endUzel;
    public Rebro(int beginInd, int endInd, Uzel startUzel) {
        this.beginInd = beginInd;
        this.endInd = endInd;
        this.startUzel = startUzel;
        this.endUzel = new Uzel(startUzel, null);
    }

    public Uzel rezRebro(Suf Suf) {
        del();
        Rebro newRebro = new Rebro(beginInd, beginInd + Suf.getLen(), Suf.getSameUzel());
        newRebro.ins();
        newRebro.endUzel.setSufUzel(Suf.getSameUzel());
        beginInd += Suf.getLen() + 1;
        startUzel = newRebro.getEndUzel();
        ins();
        return newRebro.getEndUzel();
    }

    public void ins() {
        startUzel.addRebro(beginInd, this);
    }

    public void del() {
        startUzel.delRebro(beginInd);
    }

    public int getLen() {
        return endInd - beginInd;
    }

    public int getBeginInd() {
        return beginInd;
    }

    public int getEndInd() {
        return endInd;
    }

    public void setEndInd(int endInd) {
        this.endInd = endInd;
    }

  
    public Uzel getEndUzel() {
        return endUzel;
    }

    @Override
    public String toString() {
        return endUzel.toString();
    }
}